package com.goldmantis.alitrip.controller.system;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.UserThreadLocal;
import com.goldmantis.alitrip.pojo.SysRole;
import com.goldmantis.alitrip.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 12:31 2018/2/11
 */
@RestController
@Api(description = "角色管理")
@RequestMapping("sys/role")
@RequiresAuthentication

public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 获取角色
     *
     * @param sysRole
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "角色列表", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> list(
            @ApiParam("查询对象") SysRole sysRole,
            @ApiParam("当前页") @RequestParam(defaultValue = "1") Integer offset,
            @ApiParam("每页显示数量") @RequestParam(required = false) Integer limit) {
        return ResponseEntity.ok(ReturnMessage.success(sysRoleService.select(sysRole, offset, limit)));
    }

    @GetMapping(value = "{roleId}")
    @ApiOperation(value = "根据Id获取角色", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getById(
            @ApiParam(value = "主键", required = true) @PathVariable(required = true) Long roleId) {
        return ResponseEntity.ok(ReturnMessage.success(sysRoleService.selectById(roleId)));
    }

    /**
     * 新增角色
     *
     * @param sysRole
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增角色", httpMethod = "POST")
    public ResponseEntity<ReturnMessage> post(@ApiParam("角色对象") @RequestBody @Valid SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        sysRole.setCreateBy(UserThreadLocal.get().getUserId());
        ReturnMessage returnMessage=new ReturnMessage();

        if(this.sysRoleService.codeExist(sysRole)!=null){
            returnMessage.setBusinessCode(40001);
            returnMessage.setResult("角色编码已存在！");

            return ResponseEntity.ok(returnMessage);
        }
        if(this.sysRoleService.roleNameExist(sysRole)!=null){
            returnMessage.setBusinessCode(40001);
            returnMessage.setResult("角色名称已存在！");
            return ResponseEntity.ok(returnMessage);
        }
        return ResponseEntity.ok(ReturnMessage.success(sysRoleService.insert(sysRole)));
    }

    /**
     * 编辑角色
     *
     * @param sysRole
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "编辑角色", httpMethod = "PUT")
    public ResponseEntity<ReturnMessage> put(@ApiParam("角色对象") @RequestBody @Valid SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        sysRole.setCreateBy(UserThreadLocal.get().getUserId());

        ReturnMessage returnMessage=new ReturnMessage();

        if(this.sysRoleService.codeExist(sysRole)!=null){
            returnMessage.setBusinessCode(40001);
            returnMessage.setResult("角色编码已存在！");

            return ResponseEntity.ok(returnMessage);
        }
        if(this.sysRoleService.roleNameExist(sysRole)!=null){
            returnMessage.setBusinessCode(40001);
            returnMessage.setResult("角色名称已存在！");
            return ResponseEntity.ok(returnMessage);
        }
        return ResponseEntity.ok(ReturnMessage.success(sysRoleService.update(sysRole)));
    }

    /**
     * 批量删除角色
     *
     * @param role_ids
     * @return
     */
    @DeleteMapping(value = "{role_ids}")
    @ApiOperation(value = "批量删除角色", httpMethod = "DELETE")
    public ResponseEntity<ReturnMessage> delete(@ApiParam("角色id 逗号隔开（1,2）") @PathVariable String role_ids) {
        List<Object> ids = Arrays.asList(StringUtils.split(role_ids, ","));
        return ResponseEntity.ok(ReturnMessage.success(sysRoleService.deleteByIds(ids, SysRole.class, "roleid")));
    }

    @GetMapping(value = "getBySystemId/{systemId}")
    @ApiOperation(value = "根据系统Id获取角色", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getBySystemId(
            @ApiParam(value = "系统Id", required = true) @PathVariable Long systemId) {
        return ResponseEntity.ok(ReturnMessage.success(sysRoleService.selectBySystemId(systemId)));
    }


}
